﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_TaiLieu_ViTri_Them : DevExpress.XtraEditors.XtraForm
    {
        public frm_TaiLieu_ViTri_Them()
        {
            InitializeComponent();
        }

        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaViTri;

        public int MaViTri
        {
            get { return _MaViTri; }
            set { _MaViTri = value; }
        }
        private string _TenViTri;

        public string TenViTri
        {
            get { return _TenViTri; }
            set { _TenViTri = value; }
        }
        private string _MoTa;

        public string MoTa
        {
            get { return _MoTa; }
            set { _MoTa = value; }
        }
        public void getData()
        {
            txt_MaVT.Text = Convert.ToInt32(MaViTri).ToString();
            txt_TenVT.Text = TenViTri;
            txt_MoTa.Text = MoTa;
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Vị Trữ Lưu Trữ";
                txt_MaVT.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Vị Trí Lưu Trữ";
                getData();
            }
        }
        public delegate void GetInt(int ma);
        public GetInt Get_Load;
        ViTri_BLL vitri_bll = new ViTri_BLL();
        private void frm_TaiLieu_ViTri_Them_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Luu_Click(object sender, EventArgs e)
        {
            ViTriLuuTru_Public vt = new ViTriLuuTru_Public();
            if (Flag == 0)
            {
                //Thêm
                vt.TenViTri = txt_TenVT.Text;
                vt.MoTa = txt_MoTa.Text;
                vitri_bll.insert_viTri(vt, vt);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Vị trí lưu trữ: '" +txt_TenVT.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //Sửa
                vt.MaViTri =Convert.ToInt32( txt_MaVT.Text);
                vt.TenViTri = txt_TenVT.Text;
                vt.MoTa = txt_MoTa.Text;
                vitri_bll.update_viTri(vt,vt, vt);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Vị trí lưu trữ: '" +TenViTri.ToString() + "' thành: '" + txt_TenVT.Text+ "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Get_Load(1);
            txt_TenVT.ResetText();
            txt_MoTa.ResetText();
        }

        private void btn_HuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_MaVT_Leave(object sender, EventArgs e)
        {
            if (txt_MaVT.Text == "")
                txt_MaVT.Focus();
        }

        private void txt_MaVT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}